//
//  Files_New.m
//  IconMaster
//
//  Created by mac on 2017/8/7.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "Files_New.h"

@implementation Files_New
- (void)readADFilesIsExitOrWrite:(NSDictionary *)dict{

    //文件名 ADIDFiles
    NSString  *filename=@"ADIDFiles.plist";
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
    if(![fileManager fileExistsAtPath:filePath])
    {
//        NSLog(@"文件不存在");
         [fileManager createFileAtPath:filePath contents:nil attributes:nil];
        
    }else{
//        NSLog(@"文件存在");
        
    }
}
+ (void)getADID:(dictADID)dict{

    [IconmasterHttp GreatNetWorkingRequestType:GreatRequestType_GET andurl:GetADID andparameters:nil andcomplete:^(id json, NSError *error) {
        if ([json[@"code"] integerValue] == 200) {
            [self writefile:json[@"data"][@"admobAd"] fileName:@"ADIDFiles.plist"];
            dict(json[@"data"][@"admobAd"]);
        } else {
            //
            dict([self readFiles]);
        }
    }];
}
//ADIDFiles.plist
+(void)writefile:(NSDictionary *)string fileName:(NSString *)fileName
{
//    NSLog(@"fileName==%@",fileName);
    NSArray *paths  = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *homePath = [paths objectAtIndex:0];
    
    NSString *filePath = [homePath stringByAppendingPathComponent:fileName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if(![fileManager fileExistsAtPath:filePath]) //如果不存在
    {
//        NSLog(@"-------文件不存在，写入文件----------");
        NSError *error;
//        [string writeToFile:fileName atomically:YES];
        if([string writeToFile:fileName atomically:YES])
        {
//            NSLog(@"------写入文件------success");
        }
        else
        {
//            NSLog(@"------写入文件------fail,error==%@",error);
        }
    }
    else//追加写入文件，而不是覆盖原来的文件
    {
        
        NSError *error;
        if([string writeToFile:fileName atomically:YES])
        {
//            NSLog(@"------写入文件------success");
        }
        else
        {
//            NSLog(@"------写入文件------fail,error==%@",error);
        }

        
//        NSLog(@"-------文件存在，追加文件----------");
//        NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
//        
//        [fileHandle seekToEndOfFile];  //将节点跳到文件的末尾
//        
//        
//        NSData* stringData  = [string dataUsingEncoding:NSUTF8StringEncoding];
//        
//        [fileHandle writeData:stringData]; //追加写入数据
//        
//        [fileHandle closeFile];
    }
}
+ (NSDictionary *)readFiles{

//    NSLog(@"fileName==%@",fileName);
//    NSArray *paths  = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//    NSString *homePath = [paths objectAtIndex:0];
    NSDictionary *dict = [NSDictionary dictionary];
    NSString*filePath=[[NSBundle mainBundle] pathForResource:@"ADIDFiles"ofType:@"plist"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if(![fileManager fileExistsAtPath:filePath]) //如果不存在
    {
    
       dict = @{@"id": @"1",
                @"appId":@"ca-app-pub-8324715366985508~7732329079",
                @"iosBannerAdId":@"ca-app-pub-8324715366985508/9209062277",
                @"iosInterstitialAdId":@"ca-app-pub-8324715366985508/1491405079"};
    }else{
    
        dict =([[NSDictionary alloc] initWithContentsOfFile:filePath]);
    }
    
    return dict;
}

+ (NSDictionary *)fffff{

    return @{@"id": @"1",
             @"appId":@"ca-app-pub-8324715366985508~7732329079",
             @"bannerAdId":@"ca-app-pub-8324715366985508/9209062277",
             @"interstitialAdId":@"ca-app-pub-8324715366985508/1491405079"};
}
@end
